﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2022/1/14
 * 时间: 13:08
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace imgrename
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
		}

		int index;
		string[] files;
		string name, extname, dirname;
		void textBox1_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (label1.Text == "")
				return;
			if (e.KeyChar != (char)13) {
				return;
			}
			for(int i=0;i<files.Length;i++){
				if(i==index) continue;
				if ((files[i]== dirname + "\\" + textBox1.Text + extname) ) {
					index++;
					flushit();
					return;
				}
			}
			//MessageBox.Show("Hello~~~~");
			if (String.Compare(textBox1.Text, name) != 0) {
				pictureBox1.Image.Dispose();
				string s = dirname + "\\" + textBox1.Text + extname;
				//MessageBox.Show(s);
				
				try { 
					// Moving the file file.txt to location C:\gfg.txt 
					File.Move(files[index], s);
				
				} catch (IOException ex) { 
					return;
				} 
				files[index] = s;	
			}
			
			index++;
			flushit();
     
		}
		void flushit(){
		while (true) {
				
				if (index >= files.Length) {
					index = 0;
				}
				Image image = Image.FromFile(files[index]);
				if (null == image)
					MessageBox.Show("open file error");
				else {
					pictureBox1.Image.Dispose();
					pictureBox1.Image = image;
					label1.Text = files[index];
					dirname = Path.GetDirectoryName(files[index]);
					label3.Text = extname = Path.GetExtension(files[index]);
				
					textBox1.Text = name = Path.GetFileNameWithoutExtension(files[index]);
				
					textBox1.Focus();
					textBox1.SelectAll();
					break;
				}
			}
		}


        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] fs;
                string path = openFileDialog1.FileName;//你选中的图片的绝对路径
                pictureBox1.Image = Image.FromFile(path);
                string s = Path.GetFileName(path);
                label1.Text = s;
                string di = dirname = Path.GetDirectoryName(path);
                fs = Directory.GetFiles(di);
                //			List<string> b=new List<string>(fs);
                List<string> c = new List<string>();

                foreach (string stu in fs)
                {
                    string ss = Path.GetExtension(stu);
                    if (ss == ".jpg" || ss == ".png" || ss == ".jpeg" || ss == ".bmp")
                        c.Add(stu);
                }
                files = c.ToArray();

                for (int n = 0; n < files.Length; n++)
                {

                    if (0 == String.Compare(path, files[n]))
                    {
                        index = n;
                        break;
                    }

                }
                label1.Text = files[index];
                label3.Text = extname = Path.GetExtension(files[index]);

                textBox1.Text = name = Path.GetFileNameWithoutExtension(files[index]);

                textBox1.Focus();
                textBox1.SelectAll();
                //MessageBox.Show(dirname);
                //Console.WriteLine("{0}", Directory.GetFiles(di));//输出查找到的文件路径
            }
        }

        private void textBox1_KeyPress(object sender, KeyEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }




	}
}
